<?php

namespace Modules\Groupon\Entities;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;

class GrouponSku extends Model
{
    protected $guarded = [];
    protected $table = 'groupon_skus';

    static public $resourceName = '拼团Sku';

    protected $appends = ['attrs', 'thumb', 'price'];

    public function base_sku()
    {
        return $this->belongsTo('\Modules\Product\Entities\Sku', 'sku_id');
    }

    public function groupon()
    {
        return $this->belongsTo('\Modules\Groupon\Entities\Groupon', 'groupon_id');
    }

    public function getAttrsAttribute()
    {
        return $this->base_sku ? $this->base_sku->attrs : [];
    }

    public function getThumbAttribute()
    {
        return $this->base_sku ? $this->base_sku->thumb : NULL;
    }

    public function getPriceAttribute()
    {
        return $this->base_sku ? $this->base_sku->price : NULL;
    }


    // skuable 方法
    public function isNeedAddress()
    {
        return false;
    }

    public function calcPrice($total = 1)
    {
        return $this->selling_price * $total;
    }

    public function generateOrderSkuInfo()
    {
        $sku = $this->base_sku;
        return [
            'sku_id' => $sku->id,
            'sku_uuid' => $sku->uuid,
            'title' => $sku->title,
            'thumb' => $sku->thumb,
            'number' => $sku->number,
            'attrs' => $sku->attrs,
            'price' => $sku->price,
            'selling_price' => $this->selling_price
        ];
    }
}
